﻿using System;
using System.Collections.Generic;
using System.Data;
using SME.Food.Surperise.BLL;
using SME.Food.Surperise.Model;

namespace SME.Food.Tricon.Web.AdminJS.MenusManage
{
    public partial class MenusH : System.Web.UI.Page
    {
        public Bas_Menus_BLL Menubll = new Bas_Menus_BLL();
        public Bas_Menus_Model MenuModel = new Bas_Menus_Model();
        public Menus_Dishes_BLL MenusDishesBll = new Menus_Dishes_BLL();
        public string forwardUrl = "MenusManage/MenusList.aspx";
        public string Menus_No = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Menus_No = Request.Form["Menus_No"];
            string hid = Request.Form["hid"];
            if (hid == "add")
            {
                Modify(0);
            }
            if (hid == "update")
            {
                Modify(1);
            }
            if (Request.QueryString["type"] == "delete")
            {
                Menus_No = Request.QueryString["Menus_No"];
                if (Menus_No != null)
                {
                    if (Menubll.Delete(Menus_No) && MenusDishesBll.Delete(Menus_No))
                    {
                        Common.DwzMessage(true, false, "MenusList", "MenusList", forwardUrl);
                    }
                    else
                    {
                        Common.DwzMessage(false, false, " ", " ", " ");
                    }
                }
            }
            if (Request.QueryString["type"] == "deleteAll")
            {
                string ids = Request.Form["ids"];
                if (ids != "")
                {

                    if (Menubll.DeleteList(ids.Trim(',')) && MenusDishesBll.DeleteList(ids.Trim(',')))
                    {
                        Common.DwzMessage(true, false, "MenusList", "MenusList", forwardUrl);
                    }
                    else
                    {
                        Common.DwzMessage(false, false, " ", " ", " ");
                    }
                }
            }
        }

        public void Modify(int i)
        {
            int totalNum = 0;
            decimal totalMoney = 0;
            //Bas_Menus菜单明细
            MenuModel.Mem_No = Request.Form["User_No"];
            MenuModel.Remark = Request.Form["Remark"].TrimEnd(' ');
            MenuModel.Menus_Statue = 0;//未完成菜单

            //菜品明细
            List<Menus_Dishes_Model> addList = new List<Menus_Dishes_Model>();
            List<Menus_Dishes_Model> updateList = new List<Menus_Dishes_Model>();
            //新增的菜品编号
            string addMyMenuSelectedIds = string.IsNullOrEmpty(Request.Form["addMyMenuSelectedIds"])
                                              ? ""
                                              : Request.Form["addMyMenuSelectedIds"];
            if (!string.IsNullOrEmpty(addMyMenuSelectedIds))
            {
                addMyMenuSelectedIds = addMyMenuSelectedIds.Trim(',');
            }
            string[] hidAddPrimaryKeyIds = null;
            if (addMyMenuSelectedIds.Length > 0)
            {

                hidAddPrimaryKeyIds = addMyMenuSelectedIds.Split(',');
            }
            #region 新增时
            if (i == 0)
            {
                MenuModel.Menus_No = Menubll.GetMaxId().ToString();
                if (MenuModel.Menus_No.Length < 5)
                    MenuModel.Menus_No = MenuModel.Menus_No + "10000";
                MenuModel.Menus_Time = DateTime.Now;
                //Menus_Dishes
                if (hidAddPrimaryKeyIds != null && hidAddPrimaryKeyIds.Length > 0)
                {
                    foreach (string item in hidAddPrimaryKeyIds)
                    {
                        Menus_Dishes_Model model = new Menus_Dishes_Model();
                        model.Dishes_No = Request.Form["line[" + item + "].DishesNo"];
                        model.Num = Int32.Parse(Request.Form["lookup[" + item + "].Num"]);
                        model.Menus_No = MenuModel.Menus_No;
                        model.Dishes_Price = Int32.Parse(Request.Form["lookup[" + item + "].DishesPrice"]);
                        totalNum += model.Num;
                        totalMoney += model.Dishes_Price * model.Num;
                        addList.Add(model);
                    }
                }

                MenuModel.TotalNum = totalNum;
                MenuModel.TotalMony = totalMoney;
                if (Menubll.Add(MenuModel) > 0 && MenusDishesBll.Add(addList))
                {
                    Common.DwzMessage(true, true, "MenusList", "MenusList", forwardUrl);
                }
                else
                {
                    Common.DwzMessage(false, false, " ", " ", " ");
                }
            }
            #endregion
            #region 编辑时
            if (i == 1)
            {
                //获取菜单编号
                MenuModel.Menus_No = Menus_No;
                string hidUpd = Request.Form["hidUpd"];
                string hidDel = Request.Form["hidDel"];
                string oldMenuDishesNo = Request.Form["hidFoodIdsMyMenu"];
                
                if (oldMenuDishesNo.Length > 0)
                {
                    oldMenuDishesNo = oldMenuDishesNo.Trim(',');
                    string sqlwhere = string.Format(" Menus_No={0} and Dishes_No in ({1})", Menus_No, oldMenuDishesNo);
                    DataTable tableforNum = MenusDishesBll.GetList(sqlwhere).Tables[0];
                    if (tableforNum.Rows.Count > 0)
                    {
                        int num = 0; decimal money=0;
                        foreach (DataRow  dr in tableforNum.Rows)
                        {
                            num +=Int32.Parse(dr["Num"].ToString());
                            money += Int32.Parse(dr["Num"].ToString()) *decimal.Parse(dr["Dishes_Price"].ToString());
                        }
                        totalNum += num;
                        totalMoney += money;
                    }
                }
                
                //编辑时新增菜品
                if (hidAddPrimaryKeyIds != null && hidAddPrimaryKeyIds.Length > 0)
                {
                    foreach (string item in hidAddPrimaryKeyIds)
                    {
                        Menus_Dishes_Model model = new Menus_Dishes_Model();

                        model.Dishes_No = Request.Form["line[" + item + "].DishesNo"];
                        model.Num = Int32.Parse(Request.Form["lookup[" + item + "].Num"]);
                        model.Menus_No = MenuModel.Menus_No;
                        int price = Int32.Parse(Request.Form["lookup[" + item + "].DishesPrice"]);
                        totalNum += model.Num;
                        totalMoney += price * model.Num;
                        addList.Add(model);
                    }
                }
                //编辑时删除菜品
                string ids = "";
                if (hidDel.Length > 0)
                {
                    ids = hidDel.Trim(',');
                    ids = ids + "|" + MenuModel.Menus_No;
                }

                //编辑时修改菜品信息时只能修改菜品数量。
                if (hidUpd.Length > 0)
                {
                    string[] upds = hidUpd.Trim(',').Split(',');
                    foreach (string item in upds)
                    {
                        //先编辑后删除
                        if (ids.IndexOf(item) > -1)
                        {
                            continue;
                        }
                        string menudish = item + "|" + Menus_No;
                        //获取要修改的 菜品明细
                        Menus_Dishes_Model model = MenusDishesBll.GetModel(menudish);
                        //修改菜品数量
                       
                         string o= Request.Form["lookup[" + item + "].Num"];
                         model.Num = Int32.Parse(o);
                        totalNum += model.Num;
                        totalMoney += model.Dishes_Price * model.Num;
                        updateList.Add(model);
                    }
                }
                if (totalNum == 0)
                    MenuModel.TotalNum = Int32.Parse(Request.Form["Num"]);
                else
                    MenuModel.TotalNum = totalNum;
                if (totalMoney == 0)
                    MenuModel.TotalMony = Int32.Parse(Request.Form["Money"]);
                else
                    MenuModel.TotalMony = totalMoney;
                //update方法四个参数分别为要修改的菜单明细，新增的食材，删除的食材，修改的食材。

                if (MenusDishesBll.Update(MenuModel, addList, ids, updateList))
                {
                    Common.DwzMessage(true, true, "MenusList", "MenusList", forwardUrl);
                }
                else
                {
                    Common.DwzMessage(false, false, " ", " ", " ");
                }

            }
            #endregion
        }

    }
}